﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using WFNetLib;
using System.Threading;
using WFNetLib.StringFunc;
using Common;
using System.Diagnostics;

namespace 生命体征测试仪
{
    public partial class formMain : formBase
    {        
        public static bool bOnServer = false;
        public static UserInfoData curUser;
        public static string PhotoFileName;
        public static int PType = 0;
        public formMain()
        {
            Level1Position = new Rectangle[4];
            Level1Position[0] = new Rectangle(0, 0, 90, 70);
            Level1Position[1] = new Rectangle(934, 0, 90, 70);
            Level1Position[2] = new Rectangle(934, 698, 90, 70);
            Level1Position[3] = new Rectangle(0, 698, 90, 70);
            Level1Count = 0;
            InitializeComponent();
//             _MedicalResult MedicalResult = new _MedicalResult();
//             List<ushort> x = new List<ushort>();
//             for (int i = 9803; i < 10023; i++)
//                 x.Add((ushort)i);
//             MedicalResult.XinDianDatas = StringsFunction.ushortToHexStr(x,"");
//             MedicalResult.XinDianResult = 2;
//             MedicalResult.XinDian_XinLv = 120;
//             string post = WFJSON.JsonSerializerBySingleData<_MedicalResult>(MedicalResult);
//             _MedicalResult MedicalResult1 = WFJSON.JsonDeserializeBySingleData<_MedicalResult>(post);
        }
        Rectangle[] Level1Position;
        int Level1Count;
        private void formMain_Load(object sender, EventArgs e)
        {
#if (Web)
            btVerify.Visible = false;
            lbNet.Visible = true;
            lbID.Visible = false;
            Thread t = new Thread(ThreadServer);
            t.Start();
            //timerOnServer.Enabled = true;
#else
            btVerify.Text="输入验证码";
            lbNet.Visible=false;
            lbID.Visible=false;
            try
            {
                int used = int.Parse(SoftDataProtect.GetSoftData(1));
                int count = int.Parse(SoftDataProtect.GetSoftData(2));
                if (count != 0)
                {
                    if (used > count)
                        btVerify.Visible = true;
                    else
                    {
                        used++;
                        SoftDataProtect.SetSoftData(used.ToString(), 1);
                    }
                }
            }
            catch// (System.Exception ex)
            {
                //MessageBox.Show(ex.Message);
                btVerify.Visible = true;
            }
#endif

            iniFileOP.iniFilePath = System.Windows.Forms.Application.StartupPath + "\\MainImages\\miConfig.ini";
            int interval_time = int.Parse(iniFileOP.Read("MainImages Setting", "interval_time"));
            int text_height = int.Parse(iniFileOP.Read("MainImages Setting", "text_height"));
            int borderwidth = 670;
            int borderheight = 500;

            skinLabel1.Text = DateTime.Now.ToString("yyyy年MM月dd日 dddd HH:mm:ss ");
            timer1.Enabled = true;

            axShockwaveFlash2.AllowScriptAccess = "sameDomain";
            axShockwaveFlash2.BGColor = "#f0f0f0";
            axShockwaveFlash2.Menu = false;
            axShockwaveFlash2.WMode = "opaque";

            DataTable dt = FileOP.getDirectoryInfos("MainImages", FileOPMethod.File);
            int n=0;
            string str = "";
            string title = "";
            foreach (DataRow dr in dt.Rows)
            {
                if(    dr["content_type"].ToString()=="jpg"
                    || dr["content_type"].ToString() == "gif"
                    || dr["content_type"].ToString() == "png"
                    || dr["content_type"].ToString() == "swf")
                {
                    n++;
                    str += "MainImages/"+dr["name"] + "|";
                    title += dr["filename"] + "|";
                    if(n==9)
                        break;
                }
            }
            try
            {
                str = str.Substring(0, str.Length - 1);
                title = title.Substring(0, title.Length - 1);
                axShockwaveFlash2.FlashVars = "pics=" + str;
                axShockwaveFlash2.FlashVars += "&links=1.jpg|2.jpg|3.jpg|1.jpg|2.jpg|3.jpg|1.jpg|2.jpg|3.jpg|1.jpg|2.jpg|3.jpg";
                axShockwaveFlash2.FlashVars += "&texts=" + title;
                axShockwaveFlash2.FlashVars += "&borderwidth=" + borderwidth.ToString();
                axShockwaveFlash2.FlashVars += "&borderheight=" + borderheight.ToString();
                axShockwaveFlash2.FlashVars += "&textheight=" + text_height.ToString();
                axShockwaveFlash2.FlashVars += "&interval_time=" + interval_time.ToString();
                axShockwaveFlash2.Width = borderwidth;
                axShockwaveFlash2.Height = borderheight + text_height;
                axShockwaveFlash2.Movie = AppDomain.CurrentDomain.BaseDirectory + "pixviewer.swf";

                axShockwaveFlash2.Play();  
            }
            catch// (System.Exception ex)
            {
            	
            }
            SysConfig.OpenCOM();         
        }

        private void skinButton1_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            skinLabel1.Text = DateTime.Now.ToString("yyyy年MM月dd日 dddd HH:mm:ss ");
        }

        private void formMain_Click(object sender, EventArgs e)
        {
            ProcMouseClick(Cursor.Position);
        }
        protected override void WndProc(ref Message m)
        {
            const int WM_MOUSEDOWN = 0x0210;           
            if (m.Msg == WM_MOUSEDOWN)
            {
                ProcMouseClick(Cursor.Position);
                //MessageBox.Show(m.Msg.ToString());
                return;
            }
            base.WndProc(ref m);
        }
        void ProcMouseClick(Point p)
        {
            timerLevel.Enabled = false;
            if (Level1Position[Level1Count].Contains(p))
            {                
                Level1Count++;
                //lbNet.Text = Level1Count.ToString();
                timerLevel.Enabled = true;
                if (Level1Count >= 4)
                {
                    formMaintenance f = new formMaintenance(this);
                    f.ShowDialog();
                    Level1Count = 0;
                }
            }
            else
            {
                Level1Count = 0;
                //lbNet.Text = Level1Count.ToString();
            }
        }

        private void timerLevel_Tick(object sender, EventArgs e)
        {
            Level1Count = 0;
            //lbNet.Text = Level1Count.ToString();
        }

        private void 人员体检_Click(object sender, EventArgs e)
        {
//             formMedical fx = new formMedical();
//             fx.ShowDialog();
//             return;
            if (btVerify.Visible)
                return;
#if (Web)
            if (!formMain.bOnServer)
                return;
#endif 
            formGetID ff = new formGetID();
            ff.ShowDialog();
            if (ff.bIDOk)
            {
#if (Web)
                RegResult reg = WebWork.UserLogin(curUser.ID);
                if (reg == null)
                    MessageBox.Show("无法连接服务器");
                else
                {
                    if (reg.message == "ok")
                    {
                        if (reg.endcodeID == "1")//护士帐户
                        {
                            formChoice fx = new formChoice();
                            fx.ShowDialog();
                            if (PType == -1)
                                return;
                            formMedical f = new formMedical();
                            f.ShowDialog();
                        }
                        else
                        {
                            PType = 0;
                            formMedical f = new formMedical();
                            f.ShowDialog();
                        }
                    }
                    else//新用户，需注册
                    {
                        formUserPhoneNum f1 = new formUserPhoneNum();
                        f1.ShowDialog();
                        if (f1.phoneNum == "")
                            return;
                        curUser.PhoneNum = f1.phoneNum;
                        reg = WebWork.UserReg(curUser);
                        if(reg==null)
                            MessageBox.Show("无法连接服务器");
                        else
                        {
                            if(reg.message=="ok")
                            {
                                formMedical f = new formMedical();
                                f.ShowDialog();
                            }
                            else
                                MessageBox.Show("新建用户出错");
                        }
                    }
                }
#else
                formMedical f = new formMedical();
                f.ShowDialog();
#endif
            }
            try
            {
                string stmp = System.Windows.Forms.Application.StartupPath + "\\身份证照片";
                string file=stmp+"\\"+curUser.ID+".bmp";
                FileOP.Delete(file, FileOPMethod.File);
                file = stmp + "\\" + curUser.ID + ".jpg";
                FileOP.Delete(file, FileOPMethod.File);
            }
            catch //(System.Exception ex)
            {
            	
            }
        }

        private void skinButton2_Click(object sender, EventArgs e)
        {
//             if (btVerify.Visible)
//                 return;
//             formTimerSet f = new formTimerSet();
//             f.Show();
        }

        private void skinButton3_Click(object sender, EventArgs e)
        {
            if (btVerify.Visible)
                return;
//             formTimeRange f = new formTimeRange();
//             f.ShowDialog();
        }

        private void 体检查询_Click(object sender, EventArgs e)
        {
            if (btVerify.Visible)
                return;
#if (Web)
            if (!formMain.bOnServer)
                return;
#endif 
//             formMedicalQuery f1 = new formMedicalQuery();
//             f1.ShowDialog();
//             return;
            formGetID ff = new formGetID();
            ff.ShowDialog();
            if (ff.bIDOk)
            { 
                RegResult reg = WebWork.UserLogin(curUser.ID);
                if (reg == null)
                    MessageBox.Show("无法连接服务器");
                else
                {
                    if (reg.message == "ok")
                    {
                        if (reg.endcodeID == "1")//护士帐户
                        {
                            formChoice fx = new formChoice();
                            fx.ShowDialog();
                            if (PType == -1)
                                return;
                            formMedicalQuery f = new formMedicalQuery();
                            f.ShowDialog();
                        }
                        else
                        {
                            PType = 0;
                            formMedicalQuery f = new formMedicalQuery();
                            f.ShowDialog();
                        }
                    }
                    else
                    {
                        MessageBox.Show("你尚未注册，请进行人员体检并注册后再查询");
                    }
                }
            }
            try
            {
                string stmp = System.Windows.Forms.Application.StartupPath + "\\身份证照片";
                string file = stmp + "\\" + curUser.ID + ".bmp";
                FileOP.Delete(file, FileOPMethod.File);
                file = stmp + "\\" + curUser.ID + ".jpg";
                FileOP.Delete(file, FileOPMethod.File);
            }
            catch //(System.Exception ex)
            {

            }
        }

        private void skinButton4_Click(object sender, EventArgs e)
        {
            if (btVerify.Visible)
                return;
        }

        private void btVerify_Click(object sender, EventArgs e)
        {
#if(Web)
            formShowMachineCode f = new formShowMachineCode();
            f.ShowDialog();
#else
            formSetUseCount f = new formSetUseCount();
            f.ShowDialog();
            try
            {
                int used = int.Parse(SoftDataProtect.GetSoftData(0));
                int count = int.Parse(SoftDataProtect.GetSoftData(1));
                if (count != 0)
                {
                    if (used > count)
                        btVerify.Visible = true;
                    else
                    {
                        used++;
                        SoftDataProtect.SetSoftData(used.ToString(), 0);
                    }
                }
            }
            catch// (System.Exception ex)
            {
                //MessageBox.Show(ex.Message);
                btVerify.Visible = true;
            }
#endif
        }

        private void timerOnServer_Tick(object sender, EventArgs e)
        {
            timerOnServer.Enabled = false;
            btVerify.Visible = false;
            lbNet.Visible = true;
            if (WebWork.bInternet())
            {                
                RegResult reg=WebWork.bServer();
                if (reg!=null)
                {
                    lbNet.Text = "Internet OK Server OK";                    
                }
                else
                {
                    lbNet.Text = "Internet OK Server Error";
                }
            }
            else
            {
                lbNet.Text = "Internet Error";
                lbID.Visible = false;
            }
            //timerOnServer.Interval = 60000;
            timerOnServer.Enabled = true;
        }
        bool bThreadServer = true;
        bool bThreadServerExit = false;
        void ThreadServer()
        {
            int sleep = 1;
            int PulseTime = 5 * 60 * 1000;
            while (bThreadServer)
            {
                if (formMain.bOnServer)//心跳
                {
                    sleep--;
                    if (sleep == 0)
                    {
                        WebWork.TEPulse();
                        sleep = PulseTime;
                    }
                    Thread.Sleep(1);
                }
                else//注册
                {
                    sleep--;
                    if(sleep==0)
                    {
                        if (WebWork.bInternet())
                        {
                            RegResult reg = WebWork.bServer();
                            try
                            {                            
                                this.Invoke((EventHandler)(delegate
                                {
                                    if (reg != null)
                                    {
                                        lbNet.Text = "Internet OK Server OK";
                                        if (reg.message == "ok")
                                        {                                            
                                            try
                                            {
                                                if (SoftVerify_byMachineCode.CheckEncode_BYTE(StringsFunction.strToHexByte(reg.endcodeID, "")))
                                                {
                                                    lbID.Text = reg.zhuCeID;
                                                    lbID.Visible = true;
                                                    formMain.bOnServer = true;
                                                    btVerify.Visible = false;
                                                    sleep = PulseTime;
                                                    WebWork.Set_fujianqingdan();
                                                }
                                            }
                                            catch// (System.Exception ex)
                                            {
                                                btVerify.Visible = true;
                                            }
                                        }
                                        else
                                        {
                                            btVerify.Visible = true;
                                        }                                
                                    }
                                    else
                                    {
                                        lbNet.Text = "Internet OK Server Error";
                                    }
                                }));
                            }
                            catch (System.Exception ex)
                            {
                                Debug.WriteLine(ex.Message);
                            }
                        }
                        else
                        {
                            this.Invoke((EventHandler)(delegate
                            {
                                lbNet.Text = "Internet Error";
                            }));
                        }
                        sleep = 1*60*1000;
                    }
                    Thread.Sleep(1);
                }
            }
            bThreadServerExit = true;
        }

        private void formMain_FormClosing(object sender, FormClosingEventArgs e)
        {
            bThreadServer = false;
            while (bThreadServerExit);
            SysConfig.CloseCOM();
        }

        private void skinButton8_Click(object sender, EventArgs e)
        {

        }
    }
    public class myAxShockwaveFlash : AxShockwaveFlashObjects.AxShockwaveFlash
    {
        const int WM_MOUSEHOVER = 0x02A1;
        const int WM_MOUSELEAVE = 0x02A3;
        public const int WM_MOUSEFIRST = 0x0200; //   
        public const int WM_MOUSEMOVE = 0x0200; //移动鼠标   
        public const int WM_LBUTTONDOWN = 0x0201; //按下鼠标左键   
        public const int WM_LBUTTONUP = 0x0202; //释放鼠标左键   
        public const int WM_LBUTTONDBLCLK = 0x0203; //双击鼠标左键   
        public const int WM_RBUTTONDOWN = 0x0204; //按下鼠标右键   
        public const int WM_RBUTTONUP = 0x0205; //释放鼠标右键   
        public const int WM_RBUTTONDBLCLK = 0x0206; //双击鼠标右键   
        public const int WM_MBUTTONDOWN = 0x0207; //按下鼠标中键   
        public const int WM_MBUTTONUP = 0x0208; //释放鼠标中键   
        public const int WM_MBUTTONDBLCLK = 0x0209; //双击鼠标中键   
        public const int WM_MOUSEWHEEL = 0x020A; //当鼠标轮子转动时发送此消息个当前有焦点的控件   
        public const int WM_MOUSELAST = 0x020A;
        protected override void WndProc(ref Message m)
        {
            if (m.Msg < 0x02a4 && m.Msg > 0x0199)
                return;
//             switch (m.Msg)
//             {
//                 case WM_LBUTTONDOWN:
//                     return;
//                 case wmmol:
//                     return;
//                 case 0x0203:
//                     return;
//             }
            base.WndProc(ref m);
            //                     {
            // 
            //                         Int16 x = (Int16)m.LParam;
            //                         Int16 y = (Int16)((int)m.LParam >> 16);
            //                         Point cursorPosition = new Point(x, y);
            //                         Debug.WriteLine(cursorPosition.ToString());
            //                         return;
            //                         //MessageBox.Show("The mouse click at:" + cursorPosition.ToString());
            //                     }

        }
    }
}
